const http= require('http');
const io = require('socket.io');
const url =require('url');   //解析域名
const mysql = require('mysql');
const fs=require('fs');
const regs=require('./libs/regs');

// 数据库
let db = mysql.createPool({
    host:'localhost',
    port:'3306',
    user:'root',
    password:'',
    database:'socket'
})

// 1.创建http服务
let httpServer=http.createServer((req,res)=>{
    let {pathname,query}=url.parse(req.url,true)   //结构赋值
    console.log(pathname)
    console.log(query)
    if(pathname=='/req'){
        // 注册接口
        let {user,pass}=query;
        //1.校验数据
        let username=new RegExp(/^\w{6,32}$/);
        console.log(username)
        console.log(regs.username)
        if(!regs.username.test(user)){
            res.write(JSON.stringify({'code':1,msg:'用户名不符合规范'}));
            res.end();
        }else if(!regs.password.test(pass)){
            res.write(JSON.stringify({'code':1,msg:'密码不符合规范'}));
            res.end();
        }else{
            //2.检查用户名是否重复
            db.query(`SELECT ID FROM user_table WHERE username='${user}'`,(err,data)=>{
                if(err){
                    res.write(JSON.stringify({'code':1,msg:'数据库出错'}));
                    res.end();
                }else if(data.length>0){
                    res.write(JSON.stringify({'code':1,msg:'用户名已存在'}));
                    res.end();
                }else{
                    db.query(`INSERT INTO user_table (username,password,online) VALUES ('${user}','${pass}',0)`,err=>{
                        if(err){
                            res.write(JSON.stringify({'code':1,msg:'插入数据库出错'}));
                            res.end();
                        }else{
                            res.write(JSON.stringify({'code':0,msg:'成功'}));
                            res.end();
                        }
                    })
                }
            })
        }
        
    }else if(pathname=='/login'){

        // 登陆
        
        let {user,pass}=query;
        if(!regs.username.test(user)){
            res.write(JSON.stringify({code:1,msg:'用户名不符合规范'}))
            res.end()
        }else if(!regs.password.test(pass)){
            res.write(JSON.stringify({code:1,msg:'密码不符合规范'}))
            res.end()
        }else{
            db.query(`SElECT ID,password FROM user_table WHERE username='${user}'`,(err,data)=>{
                if(err){
                    res.write(JSON.stringify({code:1,msg:'登陆服务器出错'}))
                    res.end()
                }else if(data.length==0){
                    res.write(JSON.stringify({code:1,msg:'用户名不存在'}))
                    res.end()
                }else if(data[0].password!=pass){
                    res.write(JSON.stringify({code:1,msg:'密码错误'}))
                    res.end()
                }else{
                    db.query(`UPDATE user_table SET online=1 WHERE ID=${data[0].ID}`,err=>{
                        if(err){
                            res.write(JSON.stringify({code:1,msg:'服务器出错'}))
                            res.end()
                        }else{
                            res.write(JSON.stringify({code:0,msg:'登陆成功'}))
                            res.end()
                        }
                    })
                    
                }
            })
        }
    }else{
        fs.readFile(`www${req.url}`,(err,data)=>{
            if(err){
                res.writeHead(404);
                res.write('NOT FOUND');
            }else{
                res.write(data);
            }
            res.end();
        })
    }
})
httpServer.listen(8080);